<template>
  <div class="app-container">

    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px" size="medium" class="ry_form">
      <el-form-item label="资产编码" prop="number">
        <el-input
          v-model.trim="queryParams.number"
          placeholder="请输入资产编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资产名称" prop="name">
        <el-input
          v-model.trim="queryParams.name"
          placeholder="请输入资产名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资产分类ID" prop="categoryId">
        <el-input
          v-model.trim="queryParams.categoryId"
          placeholder="请输入资产分类ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="RFID" prop="rfid">
        <el-input
          v-model.trim="queryParams.rfid"
          placeholder="请输入RFID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌" prop="brand">
        <el-input
          v-model.trim="queryParams.brand"
          placeholder="请输入品牌"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="型号" prop="model">
        <el-input
          v-model.trim="queryParams.model"
          placeholder="请输入型号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="购置方式" prop="buyWay">
        <el-input
          v-model.trim="queryParams.buyWay"
          placeholder="请输入购置方式"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <template v-if="showMoreCondition">
        <el-form-item label="购置时间" prop="buyTime">
          <el-date-picker
              clearable
              size="small"
              v-model="queryParams.buyTime"
              type="datetime"
              value-format="yyyy-MM-ddTHH:mm:ss"
              placeholder="选择购置时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="购置金额" prop="buyPrice">
          <el-input
            v-model.trim="queryParams.buyPrice"
            placeholder="请输入购置金额"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="管理员ID" prop="adminId">
          <el-input
            v-model.trim="queryParams.adminId"
            placeholder="请输入管理员ID"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
      </template>
      <el-form-item class="flex_one tr">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button :icon="showMoreCondition ? 'el-icon-arrow-up' : 'el-icon-arrow-down'" size="mini" @click="showMoreCondition = !showMoreCondition">{{showMoreCondition ? '收起条件' : '展开条件'}}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wms:AssetItem:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wms:AssetItem:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wms:AssetItem:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['wms:AssetItem:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <WmsTable v-loading="loading" :data="AssetItemList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <el-tag :type="getStateTagType(scope.row.state)">{{ assetStateMap[scope.row.state] }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="资产编码" align="center" prop="number" v-if="columns[0].visible"/>
      <el-table-column label="资产名称" align="center" prop="name" v-if="columns[1].visible"/>
      <el-table-column label="资产分类" align="center" prop="categoryName"/>
      <el-table-column label="RFID" align="center" prop="rfid" v-if="columns[3].visible"/>
      <el-table-column label="品牌" align="center" prop="brand" v-if="columns[4].visible"/>
      <el-table-column label="型号" align="center" prop="model" v-if="columns[5].visible"/>
      <el-table-column label="购置方式" align="center" prop="buyWay" v-if="columns[6].visible"/>
      <el-table-column label="购置时间" align="center" prop="buyTime" width="180" v-if="columns[7].visible">
        <template slot-scope="scope">
            <span>{{ parseTime(scope.row.buyTime, '')}}</span>
        </template>
      </el-table-column>
      <el-table-column label="购置金额" align="center" prop="buyPrice" v-if="columns[8].visible"/>
      <el-table-column label="管理员ID" align="center" prop="adminId" v-if="columns[9].visible"/>
      
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wms:AssetItem:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wms:AssetItem:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </WmsTable>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <edit ref="editDialog" :current-row="currentRow" @sub="onEditSub"></edit>

  </div>
</template>

<script>

import { mapGetters } from 'vuex'
import { listAssetItem, getAssetItem, delAssetItem, exportAssetItem } from "@/api/asset/assetItem";
import Edit from './edit'

export default {
  name: "AssetItem",
  components: { Edit },
  computed: {
    ...mapGetters(['assetStateMap'])
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资产清单表表格数据
      AssetItemList: [],
      // 当前行数据
      currentRow: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        number: null,
        name: null,
        categoryId: null,
        rfid: null,
        brand: null,
        model: null,
        buyWay: null,
        buyTime: null,
        buyPrice: null,
        adminId: null
      },
      columns: [
            { key: 1, label: "资产编码", visible:  true  },
            { key: 2, label: "资产名称", visible:  true  },
            { key: 3, label: "资产分类ID", visible:  true  },
            { key: 4, label: "RFID", visible:  true  },
            { key: 5, label: "品牌", visible:  true  },
            { key: 6, label: "型号", visible:  true  },
            { key: 7, label: "购置方式", visible:  true  },
            { key: 8, label: "购置时间", visible:  true  },
            { key: 9, label: "购置金额", visible:  true  },
            { key: 10, label: "管理员ID", visible:  false  },
         ],
      showMoreCondition: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询资产清单表列表 */
    getList() {
      this.loading = true;
      const {pageNum, pageSize} = this.queryParams;
      const query = {...this.queryParams, pageNum: undefined, pageSize: undefined};
      const pageReq = {page: pageNum - 1, size: pageSize};
      listAssetItem(query, pageReq).then(response => {
        const { content, totalElements } = response
        this.AssetItemList = content;
        this.total = totalElements;
        this.loading = false;
      }).catch(error => {
        this.loading = false;
        console.log(error);
      });
    },
    getStateTagType(state) {
      let res = 'success';
      if (state === 1) {
        res = ''
      } else if (state === 2 || state === 3 || state === 4) {
        res = 'warning';
      } else if (state === 5) {
        res = 'danger';
      }
      return res;
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        number: null,
        name: null,
        categoryId: null,
        rfid: null,
        brand: null,
        model: null,
        buyWay: null,
        buyTime: null,
        buyPrice: null,
        adminId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.editDialog.open();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.editDialog.open(row);
    },
    onEditSub() {
      this.getList();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除资产清单表编号为"' + ids + '"的数据项？').then(function() {
        return delAssetItem(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有资产清单表数据项？').then(() => {
        this.exportLoading = true;
        return exportAssetItem(queryParams);
      }).then(response => {
        this.$download.download(response);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
